package done.easy_101_200;

import com.study.common.ListNode;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import static com.study.util.LogUtil.info;

/**
 * 203. Remove Linked List Elements
 * <p>
 * 删除链表中等于给定值 val 的所有节点。
 * <p>
 * 示例:
 * 输入: 1->2->6->3->4->5->6, val = 6
 * 输出: 1->2->3->4->5
 *
 * @author 大硕
 * 2019-03-24 9:50 PM
 **/
@Slf4j
public class RemoveLinkedListElements {

    @SuppressWarnings("all")
    public ListNode removeElements(ListNode head, int val) {
        //190324 first
        ListNode res = new ListNode();
        res.next = head;
        ListNode cur = res;
        while (cur != null && cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return res.next;
    }

    @Test
    public void test() {
        ListNode head = new ListNode(1, 2, 6, 3, 4, 5, 6);
        int val = 6;
        info(head);
        info(removeElements(head, val));
    }
}
























/*
public ListNode removeElements(ListNode head, int val) {
    if (head == null) {
        return null;
    }

    head.next = removeElements(head.next, val);
    return head.val == val ? head.next : head;
}
*/
